$(function(){
    obj.init();
});
    // layui.config({
    //     base: '/assets/layui2/lay/modules/'
    // });
var obj = new function() {
    'use strict';
    return {
        dataGrid: null,
        power: {
            star: false,
            edit: false
        },
        data: null,
        init: function () {
            this.power.star = this.power.edit = !!$('#bt_list_hide').length;

            this.dataGrid = new xhDataGrid($('#List'), 'Read/getComment');
            this.dataGrid.parseToolbar().init()
                .activeButton(['bt_list_hide', 'bt_list_del', 'bt_list_recover', 'bt_list_forbid', 'bt_list_lock', 'bt_list_unlock', 'bt_list_pass', 'bt_list_waitpass']);
            this.funObj().dataGridEvent();

            xhTool.bindDocKeyDown($('#bt_list_search'));

            this.listener();
        },
        listener: function () {
            var $this = this;
            //搜索组
            $('#bt_list_search').click(function () {
                $this.funObj().searchComment();
            });
            //恢复评论
            $('#bt_list_recover').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().dealComment(1, '确认恢复？');
            });
            //屏蔽
            $('#bt_list_hide').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().dealComment(3, '确认屏蔽？');
            });
            //删除
            $('#bt_list_del').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().dealComment(2, '确认删除？');
            });
            //禁言
            $('#bt_list_forbid').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().forbidStud();
            });
            //锁定
            $('#bt_list_lock').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().lockComment(2, '确认锁定？');
            });
            //解锁
            $('#bt_list_unlock').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().lockComment(1, '确认解锁？');
            });
            //审核通过
            // $('#bt_list_pass').click(function () {
            //     if (xhExtra.buttonDisabled($(this))) return false;
            //     $this.funObj().dealComment(4, '确认通过？');
            // });
            //加入待审池
            $('#bt_list_waitpass').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().dealComment(7, '确认加入待审池？');
            });
            //添加敏感词
            $('#bt_list_sensitive').click(function () {
                $this.funObj().addSensitive();
            });

            $this.dataGrid.onDblClickCell = function(rowIndex, field, value){
                if (field == 'articletitle') {
                    window.open($this.data['rows'][rowIndex].articleurl);
                }
            };
        },
        funObj: function () {
            var $this = this;
            return {
                dataGridEvent: function () {
                    $this.dataGrid.rowStyler = function(rowIndex, rowData){
                        if(rowData.status === '屏蔽'){
                            return 'background-color: yellow';
                        }
                    };
                    $this.dataGrid.loadFilter = function (data) {
                        if ($this.power.edit) {
                            for (var i = 0; i < data.rows.length; i++){
                                data.rows[i].operate = '<a class="comment-btn comment-pass" onclick="obj.funObj().changeStatus(' + data.rows[i].id + ', 4);">通过</a>&nbsp;&nbsp;' +
                                    ' <a class="comment-btn comment-ignore" onclick="obj.funObj().changeStatus(' + data.rows[i].id + ', 5);">不通过</a>&nbsp;&nbsp;' +
                                    ' <a class="comment-btn comment-forbid" onclick="obj.funObj().changeStatus(' + data.rows[i].id + ', 1);">屏蔽</a>';
                            }
                        }
                        return data;
                    };
                    $this.dataGrid.onLoadSuccess = function (data) {
                        $this.data = data;
                        $.each(data.rows, function (index, row) {
                            $this.funObj().renderStar(row.id);
                        })
                    };
                },
                searchComment: function () {
                    $this.dataGrid.reload({
                        search : {
                            vague: xhExtra.getValue($('#list_vague'))
                        },
                        start: xhExtra.getValue($('#list_start')),
                        end: xhExtra.getValue($('#list_end')),
                        status: xhExtra.getValue($('#list_status'))
                    });
                },
                dealComment: function (type, msg) {
                    xhExtra.askChangeStatus('Read/setCommentStatus/' + type, $this.dataGrid.getSelectValues('id'), $this.dataGrid, msg);
                },
                forbidStud: function () {
                    window.top.swal({
                        title: '选择禁言时间',
                        text: '建议不超过7天',
                        input: 'number',
                        inputPlaceholder: '天',
                        showCancelButton: true,
                        cancelButtonText: '放他一马',
                        confirmButtonText: '确认禁言',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        preConfirm: function (day) {
                            return new Promise(function (resolve, reject) {
                                if (!day) {
                                    reject('输入禁言天数！');
                                } else {
                                    xhExtra.ajaxRequest('Read/forbidStud',
                                        {forbidDay: day, studId: $this.dataGrid.getSelectValues('userid')},
                                        {success: true, table: $this.dataGrid});
                                }
                            })
                        }
                    }).then(function (groupName) {}, function(dismiss) {});
                },
                lockComment: function (type, msg) {
                    xhExtra.askChangeStatus('Read/lockComment/' + type, $this.dataGrid.getSelectValues('id'), $this.dataGrid, msg);
                },
                addSensitive: function () {
                    window.top.swal({
                        title: '添加敏感词',
                        input: 'text',
                        // inputValue: xhTool.getSelectText(),
                        inputPlaceholder: '";"分割',
                        showCancelButton: true,
                        cancelButtonText: '取消',
                        confirmButtonText: '添加',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        preConfirm: function(words) {
                            return new Promise(function (resolve, reject) {
                                if (!words) {
                                    reject('敏感词为空！');
                                } else {
                                    xhExtra.ajaxRequest('Read/addSensitive', {words: words.split(';')}, {check: true, success: true, table: $this.dataGrid});
                                }
                            })
                        }
                    }).then(function(){}, function(dismiss){});
                },
                changeStatus: function (id, status) {
                    var index = $this.dataGrid.command('getRowIndex', id);
                    $this.dataGrid.command('updateRow', {
                        index: index,
                        row: {
                            status: '<i class="fa fa-spinner fa-spin"></i>'
                        }
                    });
                    xhExtra.ajaxRequest('Read/setSingleCommentStatus', {id: id, status: status}, {check: false}, function (data) {
                        if (data.error) {
                            $this.dataGrid.command('updateRow', {
                                index: index,
                                row: {
                                    status: data.error
                                }
                            });
                            return false;
                        }
                        $this.dataGrid.command('updateRow', {
                            index: index,
                            row: data
                        });
                    });
                },
                renderStar: function(id) {
                    layui.use(['rate'], function() {
                        var target = $('.comment-star[data-id=' + id + ']');
                        layui.rate.render({
                            elem: target,
                            value: target.attr('data-star'),
                            readonly: !$this.power.star,
                            choose: function (star) {
                                xhExtra.ajaxRequest('Read/setSingleCommentStar', {id: id, star: star}, {check: true}, function (data) {
                                    $this.dataGrid.reload();
                                });
                            }
                        });
                    });
                }
            }
        }
    }
}
